// page/pack-service/pages/government-public/list-files/list-files.js
var toolUtils = require("../../../../../utils/tool-utils.js")
var httpUtils = require('../../../../../utils/https-service-utils.js')
var app = getApp()
var that
var pid
var mPageSize = 10//每页条数
var mTotalNum //总条数
var mCurrentPage = 1
var tag //1g公告公示 2惠民政策
var isGetFilterData
var attr_id
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    empty: {
      icon: '/pic/nodata.png',
      txt: '暂无数据'
    },
    dataList: [],
    checkId: 0,
    isFilter: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    isGetFilterData = false
    tag = options.tag
    pid = options.pid
    attr_id = -1
    if (tag == 1) {
      wx.setNavigationBarTitle({
        title: '公示公告',
      })
    } else {
      wx.setNavigationBarTitle({
        title: '惠民政策',
      })
    }
    that.setData({
      tag: tag
    })
    mCurrentPage = 1
    getData()
  },
  onItemClick: function (e) {
    var item = e.currentTarget.dataset.item
    toolUtils.pageTo("/page/pack-service/pages/government-public/file-details/file-details?news_id=" + item.news_id + "&tag=" + tag)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    getData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (mTotalNum > mPageSize * mCurrentPage) {
      ++mCurrentPage
      that.setData({
        loadData: {
          searchLoading: true, //"上拉加载"的变量，默认false，隐藏  
          searchLoadingComplete: false  //“没有数据”的变量，默认false，隐藏  
        }
      })
      getData()
    } else {
      that.setData({
        loadData: {
          searchLoading: false, //"上拉加载"的变量，默认false，隐藏  
          searchLoadingComplete: true  //“没有数据”的变量，默认false，隐藏  
        }
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //关闭分类
  cloesFilter:function(){
    isGetFilterData = false
    that.setData({
      isFilter: false
    })
  },
  //筛选
  onFilter: function () {
    that.setData({
      isFilter: !that.data.isFilter
    })
    if (!isGetFilterData){
      getAttrList()
    }
  },
  //分类列表点击
  onFilterItem: function (e) {
    var index = e.currentTarget.dataset.index
    var item = e.currentTarget.dataset.item
    attr_id = item.attr_id
    that.setData({
      checkId: index,
      filterName: item.attr_value,
      isFilter: !that.data.isFilter
    })
    mCurrentPage = 1
    getData()
  }
})
//获取列表数据
function getData() {
  var params = {}
  var StreetInfo = app.getStreetInfo()
  if (StreetInfo && StreetInfo.group_id) {
    params.group_id = StreetInfo.group_id
  } else {
    toolUtils.showFailToast("group_id不能为空")
    return
  }
  params.pid = pid
  params.current_page = mCurrentPage
  params.page_size = mPageSize
  if (attr_id != -1){
    params.attr_id = attr_id
  }
  httpUtils.getZwgkNews(params, function (res) {
    mTotalNum = res.total_num
    var list = that.data.dataList
    if (mCurrentPage == 1) {
      list = []
    }
    list = list.concat(res.list)
    if (list.length == mTotalNum) {
      that.setData({
        dataList: list,
        loadData: {
          searchLoading: false, //"上拉加载"的变量，默认false，隐藏  
          searchLoadingComplete: true  //“没有数据”的变量，默认false，隐藏  
        }
      })
    } else {
      that.setData({
        dataList: list,
        loadData: {
          searchLoading: false, //"上拉加载"的变量，默认false，隐藏  
          searchLoadingComplete: false  //“没有数据”的变量，默认false，隐藏  
        }
      })
    }
    wx.stopPullDownRefresh()
  }, function (e) {
    if (mCurrentPage > 1) {
      --mCurrentPage
    }
    that.setData({
      loadData: {
        searchLoading: false, //"上拉加载"的变量，默认false，隐藏  
        searchLoadingComplete: false  //“没有数据”的变量，默认false，隐藏  
      }
    })
    wx.stopPullDownRefresh()
  })
}
//获取分类数据
function getAttrList() {
  httpUtils.getZwgkAttr(pid, {}, function (res) {
    isGetFilterData = true
    if (res.length>0){
      var qb = {
        attr_id: -1,
        attr_value: "全部"
      }
      res.unshift(qb)
    }
    that.setData({
      list:res
    })
  }, function (e) {

  })
}